package leetcode_top;

import org.junit.Test;

public class Ex91 {
    class Solution {
        //202 02不算是一种解码方式
        public int numDecodings(String s) {
            int len;
            if (s == null || (len = s.length()) == 0) return 0;
            char[] cs = s.toCharArray();
            if (cs[0] == '0') return 0;
            else if (len == 1) return 1;
            int[] f = new int[len];
            f[0] = 1;
            f[1] = cs[1] != '0' && test(cs[0], cs[1]) ? 2 : 1;

            for (int i = 2; i < len; i++) {
                if (cs[i] == '0') {
                    if (cs[i - 1] == '0' || cs[i - 1] > '2') return 0;
                    f[i] = f[i - 2];
                } else {
                    f[i] = f[i - 1];
                    if (test(cs[i - 1], cs[i])) f[i] += f[i - 2];
                }
            }

            return f[len - 1];
        }

        public boolean test(char c1, char c2) {
            if (c1 == '1') return true;
            else if (c1 == '2' && c2 >= '0' && c2 <= '6') return true;
            else return false;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.numDecodings("114174758715150561651562314141547124314123"));
        
    }

    public int rand() {
        return Math.random() >= 0.5 ? 1 : 0;
    }

    public int func() {
        int num = 0;
        for (int i = 0; i < 4; i++) {
            num += (1 << i) * rand();
        }
        if (num >= 10) {
            return func();
        } else {
            if (num < 3) return 1;
            else return 0;
        }
    }

    @Test
    public void test1() {
        int count0 = 0;
        int count1 = 0;
        for (int i = 0; i < 100_0000; i++) {
            if (func() == 1) count1++;
            else count0++;
        }
        System.out.println(count0 / (double)(count0 + count1));
    }
}
